package icom.acm.manager;
/**
 * @author shench
 */
import icom.acm.bean.T_USERSET;
import icom.acm.util.DBConnect;

import java.util.ArrayList;
import java.util.List;

public class T_UserSetMgr {
	public List<T_USERSET> selectByPid0(int pid, DBConnect db){
		String sql = "SELECT US_ID, US_PID, US_ORDER, US_NAME, US_URL" +
				" FROM T_USERSET WHERE US_PID="+pid+" ORDER BY US_ORDER";
		List<T_USERSET> list = new ArrayList<T_USERSET>();		
		db.prepareStatement(sql);
System.out.println(sql);		
		db.excuteQuery();
		try {
			while (db.next()){
				T_USERSET us = new T_USERSET();
				us.setUS_ID(db.getInt("US_ID"));
				us.setUS_PID(db.getInt("US_PID"));
				us.setUS_ORDER(db.getInt("US_ORDER"));
				us.setUS_NAME(db.getString("US_NAME"));
				us.setUS_URL(db.getString("US_URL"));
				list.add(us);
			}
		} catch (Exception e) {e.printStackTrace();db.close();} finally{}
		return list;		
	}	
	
	public List<T_USERSET> selectRoot0(DBConnect db){
		String sql = "SELECT US_ID, US_PID, US_ORDER, US_NAME, US_URL" +
				" FROM T_USERSET WHERE US_PID=-1 ORDER BY US_ORDER";
		List<T_USERSET> list = new ArrayList<T_USERSET>();		
		db.prepareStatement(sql);
		
		db.excuteQuery();
		try {
			while (db.next()){
				T_USERSET us = new T_USERSET();
				us.setUS_ID(db.getInt("US_ID"));
				us.setUS_PID(db.getInt("US_PID"));
				us.setUS_ORDER(db.getInt("US_ORDER"));
				us.setUS_NAME(db.getString("US_NAME"));
				us.setUS_URL(db.getString("US_URL"));
				list.add(us);
			}
		} catch (Exception e) {e.printStackTrace();db.close();} finally{}
		return list;		
	}
	// 2级树
	public List<T_USERSET> toTreeList(){
		DBConnect db = new DBConnect();
		List<T_USERSET> list = selectRoot0(db);
		List<T_USERSET> res = new ArrayList<T_USERSET>();
		for (int i=0;i<list.size();i++){			
			T_USERSET us = list.get(i);
			int pid = us.getUS_ID();
			res.add(us); System.out.println(us.getUS_NAME());
			List<T_USERSET> l = selectByPid0(pid, db);
			for (int j=0;j<l.size();j++){
				T_USERSET u = l.get(j);
				res.add(u); System.out.println(u.getUS_NAME());
			}
		}		
		db.close();
		return res;
	}	
}
